﻿package  scripts.twitterPage{
	
	import flash.display.MovieClip;
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.display.DisplayObject;
	import scripts.ClickableText;
	import flash.net.navigateToURL;
	
	public class SummaryBtn extends MovieClip {
		private var bg:MovieClip;
		private var image:Loader;
		private var xml:XML;
		private var ac_name:ClickableText;
		private var screen_name:ClickableText;
		private var summary_text:ClickableText;
		private var temp:String;
		private var container:MovieClip;
		private var cover_all:MovieClip;
		private var image_requestURL:String;
		private var twitterUrl:String;
		public function SummaryBtn() {
			// constructor code
			this.useHandCursor=true;
			this.buttonMode=true;
			createBG();
		}
		private function createBG():void{
			bg=new greybg();
			bg.visible=false;
			container=new MovieClip();
			cover_all=new greybg();
			cover_all.name="btn__summary";
			cover_all.alpha=0;
			twitterUrl="http://www.twitter.com/";
			addChild(bg);
			addChild(container);
			addChild(cover_all);
			cover_all.addEventListener(MouseEvent.ROLL_OVER,mouseOver_handler);
			cover_all.addEventListener(MouseEvent.CLICK,mouseClick_handler);

			this.graphics.moveTo(0, 114);
			this.graphics.lineStyle(1, 0xCCCCCC);
			this.graphics.lineTo(569, 114);
		}
		private function createIMG():void{
			var xmlPath:String="";
			/*if(xml.hasOwnProperty("retweeted_status"))
				xmlPath=image_requestURL+xml.retweeted_status.user.profile_image_url;
			else
			*/
			xmlPath=image_requestURL+xml.user.profile_image_url;
			xmlPath=xmlPath.replace("http://", "http:/");
			
			var dataRequest:URLRequest = new URLRequest(xmlPath);
			image=new Loader();
			image.x=8;
			image.y=8;
			image.load(dataRequest);
			container.addChild(image);
			image.contentLoaderInfo.addEventListener(Event.COMPLETE,imageLoadingCompleteHandler);
		}
		private function mouseOver_handler(e:MouseEvent):void{
			
			bg.visible=true;
			cover_all.addEventListener(MouseEvent.ROLL_OUT,mouseOut_handler);
		}
		private function mouseOut_handler(e:MouseEvent):void{
			bg.visible=false;
			cover_all.removeEventListener(MouseEvent.ROLL_OUT,mouseOut_handler);
		}
		private function mouseClick_handler(e:MouseEvent):void{
			navigateToURL(new URLRequest(twitterUrl+xml.user.screen_name),"_blank");
		}
		private function imageLoadingCompleteHandler(e:Event):void{
			
			//addChild(e.target.content);
		}
		public function reset(summary_xml:XML,imageRequestIndex:int):void{
			image_requestURL="getImage"+imageRequestIndex+".php?url=";
			xml=summary_xml;
			//trace(xml+"----------------------------------------------------------------------------------------");
			if(image!=null||ac_name!=null||screen_name!=null||summary_text!=null){
				
				while (container.numChildren)
					container.removeChildAt(0);

				summary_text=screen_name=ac_name=null;
				image=null;
			}
			createIMG();
			createAccountNameText();
			createScreenNameText();
			createSummaryText();
			
			
		}
		private function createAccountNameText():void{
			//temp=(xml.hasOwnProperty("retweeted_status"))?xml.retweeted_status.user.name:xml.user.name;
			temp=xml.user.name;
			ac_name=new ClickableText(temp,false,"",200);
			ac_name.setFontSize(16);
			ac_name.setColour(0x000000);
			ac_name.setBold=true;
			ac_name.x=60;
			ac_name.y=5;
			container.addChild(ac_name);
		}
		private function createScreenNameText():void{
			//temp=(xml.hasOwnProperty("retweeted_status"))?"@"+xml.retweeted_status.user.screen_name:"@"+xml.user.screen_name;
			temp="@"+xml.user.screen_name;
			screen_name=new ClickableText(temp,false,"",300);
			screen_name.setFontSize(14);
			screen_name.setColour(0x666666);
			screen_name.x=ac_name.x+ac_name.width+5;
			screen_name.y=5;
			container.addChild(screen_name);			
		}
		private function createSummaryText():void{
			temp=xml.text;
			//temp=(xml.hasOwnProperty("retweeted_status"))?xml.retweeted_status.text:xml.text;
			summary_text=new ClickableText(temp,false,"",500);
			summary_text.setFontSize(14);
			summary_text.setColour(0x666666);
			summary_text.setHeight(60);
			summary_text.x=60;
			summary_text.y=28;
			container.addChild(summary_text);
		}
		
		
	}
	
}
